package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.hqyj.mapper.GoodsModelMapper;
import com.hqyj.pojo.GoodsModel;
import com.hqyj.service.GoodsModelService;
import com.hqyj.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsModelServiceImpl
 * @Description TODO
 * @Author ZhuYouWan
 * @Date 2022/8/30 20:23
 * @Version 1.0
 **/
@Service
public class GoodsModelServiceImpl implements GoodsModelService {
    private static final int CACHE_LIFE_TIME = 60*10;

    @Autowired
    private GoodsModelMapper goodsModelMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Map<String, Object> findGoodsModelAll(int pageNum, int pageSize, String condition) {
        Map<String,Object> map = new HashMap<>();
        String key = "goodsModelAll" + pageNum;

        if (!"".equals(condition)){
            PageHelper.startPage(pageNum,pageSize);
            List<GoodsModel> goodsModelList = goodsModelMapper.findAllGoodsModel(condition);
            PageInfo<GoodsModel> goodsModelPageInfo = new PageInfo<>(goodsModelList);
            map.put("code",200);
            map.put("modelInfo",goodsModelPageInfo);
        }
        else if (!redisUtil.hasKey(key)){
            PageHelper.startPage(pageNum,pageSize);
            List<GoodsModel> goodsModelList = goodsModelMapper.findAllGoodsModel(condition);
            PageInfo<GoodsModel> goodsModelPageInfo = new PageInfo<>(goodsModelList);
            map.put("code",200);
            map.put("modelInfo",goodsModelPageInfo);
            redisUtil.hmset(key,map,CACHE_LIFE_TIME);
        }else {
            map = redisUtil.hmget(key);
        }

        return map;
    }

    @Override
    public Map<String, Object> deleteGoodsModelById(int mId) {
        Integer ret = goodsModelMapper.deleteGoodsModelById(mId);
        Map<String,Object> map = new HashMap<String,Object>();
        if (goodsModelMapper.findGoodsContactByMidCount(mId)>0){
            goodsModelMapper.updateGoodsContactBymId(mId);
            map.put("cascadeState",true);
        }else {
            map.put("cascadeState",false);
        }
        if (ret>0){
            delCache();
            redisUtil.del(redisUtil.keysLike("goodsAll"));
            map.put("code",200);
            map.put("message","删除成功");
        }else {
            map.put("code",-1);
            map.put("message","删除失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> insertGoodsModel(GoodsModel goodsModel) {
        Integer ret = goodsModelMapper.insertGoodsModel(goodsModel);
        Map<String,Object> map = new HashMap<>();
        if (ret>0){
            delCache();
            redisUtil.del(redisUtil.keysLike("goodsAll"));
            map.put("code",200);
            map.put("message","添加成功");
        }else {
            map.put("code",-1);
            map.put("message","添加失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> findGoodsModelById(int mId) {
        GoodsModel goodsModelById = goodsModelMapper.findGoodsModelById(mId);
        Map<String,Object> map = new HashMap<>();
        if (goodsModelById != null) {
            map.put("code",200);
            map.put("goodsModel",goodsModelById);
        }else {
            map.put("code",-1);
            map.put("message","查询失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> findGoodsModelBymType(String mType) {
        GoodsModel goodsModelBymType = goodsModelMapper.findGoodsModelBymType(mType);
        Map<String,Object> map = new HashMap<>();
        if (goodsModelBymType!=null) {
            map.put("code",200);
            map.put("goodsModel",goodsModelBymType);
        }else {
            map.put("code",-1);
            map.put("message","查询失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> updateGoodsModelById(GoodsModel goodsModel) {
        Integer ret = goodsModelMapper.updateGoodsModelById(goodsModel);
        Map<String,Object> map = new HashMap<>();
        if (ret>0){
            delCache();//删掉缓存
            redisUtil.del(redisUtil.keysLike("goodsAll"));
            map.put("code",200);
            map.put("message","更新成功");
        }else {
            map.put("code",-1);
            map.put("message","更新失败");
        }
        return map;
    }

    public void delCache(){
        redisUtil.del(redisUtil.keysLike("goodsModelAll"));
    }
}
